package com.lg.atp.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lg.atp.dao.DeviceInfoDao;
import com.lg.atp.entity.TblDeviceInfo;
import com.lg.atp.service.IDeviceInfoService;
import com.lg.atp.utils.DeviceConstants;
import com.lg.atp.vo.device.MacacaDeviceInfo;
import com.lg.atp.ws.WSClient;

@Service("iDeviceInfoService")
public class DeviceInfoServiceImpl implements IDeviceInfoService{

	private Log log = LogFactory.getLog(DeviceInfoServiceImpl.class);
	@Autowired
	private DeviceInfoDao deviceInfoDao;
	
	private @Value("${relibleMaster}") String relibleMaster;
	
	@Override
	public TblDeviceInfo geTblDeviceInfoById(Long deviceId) {
		return deviceInfoDao.selectByPrimaryKey(deviceId.intValue());
	}

	@Override
	public PageInfo<TblDeviceInfo> getDeviceListCanbeUsed(int pageIndex,int pageSize) {
		TblDeviceInfo device = new TblDeviceInfo();
		device.setDeviceStatus(DeviceConstants.DEVICE_STATUS_VALID);
		PageHelper.startPage(pageIndex, pageSize);
		List<TblDeviceInfo> applist = deviceInfoDao.getDeviceList(device);
		PageInfo<TblDeviceInfo> list = new PageInfo<TblDeviceInfo>(applist);
		return list;
		
	}

	@Override
	public void saveDeviceList4Macaca() {
		WSClient client = new WSClient(relibleMaster);
		try {
			List<MacacaDeviceInfo> macacaList = client.getDeviceInfos();
			List ids = new ArrayList<Integer>();
			for (int i = 0; i < macacaList.size(); i++) {
				MacacaDeviceInfo macacaDeviceInfo = macacaList.get(i);
				
				
				TblDeviceInfo deviceInfo = this.geTblDeviceInfoBySerialNumber(macacaDeviceInfo.getSerialNumber());
				if (deviceInfo!=null) {
						deviceInfo.setUdid(macacaDeviceInfo.getId());
						deviceInfo.setSlaveIp(macacaDeviceInfo.getSlaveIP());
						Integer status = macacaDeviceInfo.getStatus();
						if (DeviceConstants.DEVICE_STATUS_VALID.equals(status)) {
							deviceInfo.setDeviceStatus(DeviceConstants.DEVICE_STATUS_VALID);
						}else if (DeviceConstants.DEVICE_STATUS_UN_CONNECT.equals(status)){
							deviceInfo.setDeviceStatus(DeviceConstants.DEVICE_STATUS_USING); 
						}else if (DeviceConstants.DEVICE_STATUS_MAINTAIN.equals(status)){
							deviceInfo.setDeviceStatus(DeviceConstants.DEVICE_STATUS_UN_CONNECT);
						}else if (DeviceConstants.DEVICE_STATUS_USING.equals(status)){
							deviceInfo.setDeviceStatus(DeviceConstants.DEVICE_STATUS_MAINTAIN);
						}
						deviceInfoDao.updateByPrimaryKey(deviceInfo);
				} else {
					deviceInfo = new TblDeviceInfo();
					deviceInfo.setUdid(macacaDeviceInfo.getId());
					deviceInfo.setSerialNumber(macacaDeviceInfo.getSerialNumber());
					deviceInfo.setPlatform(macacaDeviceInfo.getPlantform());
					deviceInfo.setSystemVersion(macacaDeviceInfo.getReleaseVersion());
					deviceInfo.setModel(macacaDeviceInfo.getModel());
					deviceInfo.setBrand(macacaDeviceInfo.getBrand());
					deviceInfo.setSlaveIp(macacaDeviceInfo.getSlaveIP());
					Integer status = macacaDeviceInfo.getStatus();
					if (DeviceConstants.DEVICE_STATUS_VALID.equals(status)) {
						deviceInfo.setDeviceStatus(DeviceConstants.DEVICE_STATUS_VALID);
					}else if (DeviceConstants.DEVICE_STATUS_UN_CONNECT.equals(status)){
						deviceInfo.setDeviceStatus(DeviceConstants.DEVICE_STATUS_USING);
					}else if (DeviceConstants.DEVICE_STATUS_MAINTAIN.equals(status)){
						deviceInfo.setDeviceStatus(DeviceConstants.DEVICE_STATUS_UN_CONNECT);
					}else if (DeviceConstants.DEVICE_STATUS_USING.equals(status)){
						deviceInfo.setDeviceStatus(DeviceConstants.DEVICE_STATUS_MAINTAIN);
					}
					deviceInfo.setAutoType(DeviceConstants.HAND_TEST_FLAG);
					deviceInfo.setCreateTime(new Date());
					deviceInfo.setDeviceType(1);
					deviceInfo.setScreenWidth(Integer.valueOf(macacaDeviceInfo.getScreenWidth()));
					deviceInfo.setScreenHeight(Integer.valueOf(macacaDeviceInfo.getScreenHeight()));
					deviceInfoDao.insert(deviceInfo);
				}
				ids.add(deviceInfo.getId());
			}
			if(!CollectionUtils.isEmpty(ids)){
				deviceInfoDao.updateDevice(ids);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("保存设备失败", e);
		}
	}

	private TblDeviceInfo geTblDeviceInfoBySerialNumber(String serialNumber) {
		TblDeviceInfo deviceInfo = new TblDeviceInfo();
		//deviceInfo.setDeviceStatus(DeviceConstants.DEVICE_STATUS_VALID);
		deviceInfo.setSerialNumber(serialNumber);
		List<TblDeviceInfo> devicelist = deviceInfoDao.getDeviceList(deviceInfo);
		if (!CollectionUtils.isEmpty(devicelist)) {
			return devicelist.get(0);
		}
		return null;
	}

	@Override
	public void updateDevice(TblDeviceInfo tblDeviceInfo) {
		deviceInfoDao.updateByPrimaryKey(tblDeviceInfo);
	}

	@Override
	public PageInfo<TblDeviceInfo> getDeviceByPara(Map<String,Object> map){
		Integer pageIndex = map.get("pageIndex") == null?1:Integer.valueOf(map.get("pageIndex").toString());
		Integer pageSize = map.get("pageSize") == null?10:Integer.valueOf(map.get("pageSize").toString());
		String  serialNumber = map.get("serialNumber") == null?"":map.get("serialNumber").toString();
		String  brand = map.get("brand") == null?"":map.get("brand").toString();
		String model = map.get("model") == null?"":map.get("model").toString();
		TblDeviceInfo device = new TblDeviceInfo();
		device.setSerialNumber(serialNumber);
		device.setBrand(brand);
		device.setModel(model);
		PageHelper.startPage(pageIndex,pageSize);
		List<TblDeviceInfo> list = deviceInfoDao.getDeviceByPara(device);
		PageInfo<TblDeviceInfo> page = new PageInfo<TblDeviceInfo>(list);
		return page;
	}

}
